WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
folder_registry.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include "core/utils/unused.h"
22
23// std
24#include <string>
25#include <map>
26#include <vector>
27#include <iostream>
28#include <iomanip>
29
30namespace wolf
31{
35{
36 private:
37 std::map<std::string, std::string> folders_;
38
39 public:
42 static bool registerFolder(const std::string& _plugin, const std::string _folder);
43
46 static bool unregisterFolder(const std::string& _plugin);
47
50 static bool isFolderRegistered(const std::string& _plugin);
51
54 static void printAddress();
55
58 static std::vector<std::string> getRegisteredFolders();
59
62 static void appendRegisteredFolders(std::vector<std::string>& _folders);
63
64 // Singleton ---------------------------------------------------
65 // This class is a singleton. The code below guarantees this.
66 // See: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
67 private:
70 static FolderRegistry& get();
71
72 public:
76
79 void operator=(FolderRegistry const&) = delete;
80
81 private:
85
89};
90
91inline FolderRegistry::FolderRegistry()
92{
93 // std::cout << " FolderRegistry constructor " << std::endl;
94}
95
96inline FolderRegistry::~FolderRegistry()
97{
98 // std::cout << " FolderRegistry destructor " << std::endl;}
99}
100
101inline bool FolderRegistry::registerFolder(const std::string& _plugin, const std::string _folder)
102{
103 // Unregister plugin-folder if registered with different folder
104 if (get().isFolderRegistered(_plugin) and get().folders_.at(_plugin) != _folder)
105 {
106 std::cout << std::setw(31) << std::left << "FolderRegistry"
107 << " XXX unregister old '" << _plugin << "' folder: " << get().folders_.at(_plugin) << std::endl;
108 get().unregisterFolder(_plugin);
109 }
110
111 // register plugin-folder if not registered
112 if (not get().isFolderRegistered(_plugin))
113 {
114 get().folders_.insert(std::pair<std::string, std::string>(_plugin, _folder)).second;
115 std::cout << std::setw(31) << std::left << "FolderRegistry"
116 << " <-- registered '" << _plugin << "' folder: " << _folder << std::endl;
117 return true;
118 }
119
120 return false;
121}
122
123inline bool FolderRegistry::unregisterFolder(const std::string& _plugin)
124{
125 if (get().isFolderRegistered(_plugin))
126 return get().folders_.erase(_plugin) == 1;
127 else
128 std::cout << std::setw(31) << std::left << "FolderRegistry"
129 << " ERROR trying to unregister " << _plugin << ": Not registered." << std::endl;
130 return false;
131}
132
133inline bool FolderRegistry::isFolderRegistered(const std::string& _plugin)
134{
135 return get().folders_.count(_plugin);
136}
137
138inline FolderRegistry& FolderRegistry::get()
139{
140 static FolderRegistry instance_;
141 return instance_;
142}
143
145{
146 std::cout << "FolderRegistry address: " << &get() << std::endl;
147}
148
149inline std::vector<std::string> FolderRegistry::getRegisteredFolders()
150{
151 std::vector<std::string> vec;
152 for (auto pair : get().folders_) vec.push_back(pair.second);
153 return vec;
154}
155
156inline void FolderRegistry::appendRegisteredFolders(std::vector<std::string>& _folders)
157{
158 for (auto pair : get().folders_) _folders.push_back(pair.second);
159}
160
161} // namespace wolf
162
163#define WOLF_REGISTER_FOLDER(plugin, folder) \
164 namespace \
165 { \
166 const bool WOLF_UNUSED folder##Registered = FolderRegistry::registerFolder(plugin, folder); \
167 }
Registry (singleton) of the folders of the plugins, e.g. with their schema files.
Definition: folder_registry.h:35
static void printAddress()
Prints the address of the singleton instance.
Definition: folder_registry.h:144
static std::vector< std::string > getRegisteredFolders()
Returns all the registered folders.
Definition: folder_registry.h:149
static bool isFolderRegistered(const std::string &_plugin)
Returns whether the folder of a plugin is registered.
Definition: folder_registry.h:133
FolderRegistry(const FolderRegistry &)=delete
Copy constructor (deleted: singleton)
static bool registerFolder(const std::string &_plugin, const std::string _folder)
Registers the folder of a plugin (replacing a different one). Returns whether it was registered.
Definition: folder_registry.h:101
static void appendRegisteredFolders(std::vector< std::string > &_folders)
Appends all the registered folders to a vector.
Definition: folder_registry.h:156
static bool unregisterFolder(const std::string &_plugin)
Unregisters the folder of a plugin. Returns whether it was unregistered.
Definition: folder_registry.h:123
void operator=(FolderRegistry const &)=delete
Copy assignment (deleted: singleton)
Definition: capture_base.h:23